<?php
class ActivityAction extends HomeAction{
	/**
	 * 近期活动列表
	 */
	public function index(){
		$Activity = M('Activity');
		$field = "activity.state,activity.name,user.username,activity.start_time,activity.end_time,activity.min_people,activity.max_people,
		activity.min_age,activity.max_age,activity.id,activity.scenic_images";
		$where = "1 AND activity.uid={$_SESSION['uid']}";
		$order = "activity.create_time DESC";
		$limit = C('PAGE_COUNT');
		$count = $Activity->where($where)->count();
		$Page = new Page($count, C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$join[] = "LEFT JOIN user ON user.uid=activity.uid";
		$list = $Activity->where($where)->limit($limit)->join($join)->field($field)->order($order)->select();
		foreach($list as &$v){
			$v['thumb'] = array_shift(json_decode($v['scenic_images']));
		}
		//echo $_SERVER[REQUEST_TIME];
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 修改活动
	 */
	public function update(){
		$id=$_REQUEST['id'];
		$Activity = D('Activity');
		if($_POST){
			$_POST['scenic_images'] = json_encode($_POST['images']);
			$_POST['start_time'] = strtotime($_POST['start_time'].':00');
			$_POST['end_time'] = strtotime($_POST['end_time'].':00');
			unset($_POST['images']);
			$Activity->save($_POST);
			$this->success("修改成功");
		}else{
			$activity_info = $Activity->where("id={$id}")->find();
			$activity_info['scenic_images'] = json_decode($activity_info['scenic_images']);
			$this->assign('activity_info',$activity_info);
			$this->display();
		}
	}
	/**
	 * 活动详情
	 */
	public function view(){
		$id = $_GET['id'];
		$Activity = M('Activity');
		$activity_info = $Activity->where("id={$id}")->find();
		$activity_info['thumb'] = array_shift(json_decode($activity_info['scenic_images']));
		$this->assign('activity_info',$activity_info);
		$Participation = M('participation');
		$join[] = "LEFT JOIN activity ON activity.id=participation.acid";
		$join[] = "LEFT JOIN user ON participation.uid=user.uid";
		$where = "activity.uid={$_SESSION['uid']}";
		$field = "participation.uid,user.username,participation.register_time";
		$user_list = $Participation->where($where)->join($join)->field($field)->select();
		$this->assign('user_list',$user_list);
		
		$this->display();
	}
	/**
	 * 我参加的活动
	 */
	public function join(){
		$Activity = M('Activity');
		$field = "activity.state,activity.name,user.username,activity.start_time,activity.end_time,activity.min_people,activity.max_people,
		activity.min_age,activity.max_age,activity.id,activity.scenic_images";
		$where = "1 AND participation.uid={$_SESSION['uid']}";
		$join[] = "LEFT JOIN user ON user.uid=activity.uid";
		$join[] = "LEFT JOIN participation ON participation.acid=activity.id";
		$order = "activity.create_time DESC";
		$limit = C('PAGE_COUNT');
		$count = $Activity->where($where)->join($join)->count();
		$Page = new Page($count, C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		
		$list = $Activity->where($where)->limit($limit)->join($join)->field($field)->order($order)->select();
		foreach($list as &$v){
			$v['thumb'] = array_shift(json_decode($v['scenic_images']));
		}
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 删除活动
	 */
	public function del(){
		$id = $_GET['id'];
		$Activity = M('Activity');
		$where="uid={$_SESSION['uid']} AND id={$id}";
		$Activity->where($where)->delete();
		$this->success("删除成功");
	}
	/**
	 * 上传照片
	 */
	public function upload(){
		$id = $_REQUEST['id'];
		if($_GET){
			$Activity = M('Activity');
			$activity_info = $Activity->where("id={$id}")->find();
			$activity_info['picture_urls'] = json_decode($activity_info['picture_urls']);
			$this->assign('activity_info',$activity_info);
			$this->display();
		}elseif($_POST){
			$data['picture_urls'] = json_encode($_POST['images']);
			$data['id']			  = $_POST['id'];
			$Activity = D('Activity');
			if($Activity->create($data)){
				$Activity->save();
				$this->success('修改成功');
			}else{
				$this->error($Activity->getError());	
			}
		}
	}
}